package com.baidu.fenfayun.demo.home
import android.support.v4.content.ContextCompat
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.widget.*
import android.view.*
import android.widget.*
import android.support.v4.content.res.ResourcesCompat
import android.text.*
import com.qmuiteam.qmui.util.QMUIResHelper
import com.qmuiteam.qmui.widget.QMUITabSegment
import com.baidu.fenfayun.demo.base.BaseFragment
import com.baidu.fenfayun.demo.util.*

import android.graphics.Color;
import net.steamcrafted.materialiconlib.*
import com.baidu.yun.gameline.R
import java.util.HashMap
import android.content.*
import android.os.*
import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion

import com.arlib.floatingsearchview.FloatingSearchView
import android.util.Log
import kotlinx.android.synthetic.main.fragment_search.*
import com.baidu.fenfayun.demo.base.BaseRecyclerAdapter
import com.baidu.fenfayun.demo.base.RecyclerViewHolder
import com.baidu.fenfayun.demo.model.*
import kotlin.collections.*

class SearchFragment :BaseFragment() {

   class ItemAdapter(ctx:Context ,  datas:List<UserData>) :BaseRecyclerAdapter<UserData>(ctx,datas) {

      public override fun getItemLayoutId(viewType:Int):Int {
         return R.layout.home_item_layout
      }

      public override fun bindData(holder:RecyclerViewHolder , position:Int , item:UserData) {
         holder.getTextView(R.id.item_name).setText(item.username)
         holder.getImageView(R.id.item_icon).setImageResource(R.mipmap.icon_tabbar_lab)
      }
   }

   val TAG = "test"

   var mLastQuery = ""
   protected override fun onCreateView():View  {
      val layout = (LayoutInflater.from(getActivity()).inflate(R.layout.fragment_search, null)) as FrameLayout
      return layout
   }
   override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
      initPagers()
   }
   private fun initPagers() {

      floating_search_view.setOnQueryChangeListener(object:FloatingSearchView.OnQueryChangeListener {
         override  fun onSearchTextChanged(oldQuery:String , newQuery:String ) {

            //get suggestions based on newQuery

            //pass them on to the search view
            //mSearchView.swapSuggestions(newSuggestions);
            if (!oldQuery.equals("") && newQuery.equals("")) {
               floating_search_view.clearSuggestions()
            } else {
               floating_search_view.showProgress()
               floating_search_view.swapSuggestions(listOf(ColorSuggestion("someone"),ColorSuggestion("sometwo")))
               floating_search_view.hideProgress()
            }
         }
      })

      floating_search_view.setOnSearchListener(object:FloatingSearchView.OnSearchListener {
         override fun onSuggestionClicked(searchSuggestion:SearchSuggestion ) {

            val adapter = ItemAdapter(getContext(), UserDB().findAll())
            search_results_list.setAdapter(adapter)
            search_results_list.setLayoutManager(LinearLayoutManager(getContext()))


            mLastQuery = searchSuggestion.getBody()
            Log.d(TAG, "onSuggestionClicked()"+mLastQuery)
         }

         override fun onSearchAction(query:String ) {

            mLastQuery = query
            val adapter = ItemAdapter(getContext(), UserDB().findAll())
            search_results_list.setAdapter(adapter)
            search_results_list.setLayoutManager(LinearLayoutManager(getContext()))

            Log.d(TAG, "onSearchAction()"+query);
         }
      })


      floating_search_view.setOnFocusChangeListener(object:FloatingSearchView.OnFocusChangeListener {
         public override fun onFocus() {

            //show suggestions when search bar gains focus (typically history suggestions)
            floating_search_view.swapSuggestions(listOf(ColorSuggestion("somefocus")))

            Log.d(TAG, "onFocus()")
         }

         public override fun onFocusCleared() {

            //set the title of the bar so that when focus is returned a new query begins
            floating_search_view.setSearchBarTitle(mLastQuery)

            //you can also set setSearchText(...) to make keep the query there when not focused and when focus returns
            //mSearchView.setSearchText(searchSuggestion.getBody());

            Log.d(TAG, "onFocusCleared()")
         }
      })

    floating_search_view.setOnBindSuggestionCallback(object:SearchSuggestionsAdapter.OnBindSuggestionCallback {
            public override fun onBindSuggestion(suggestionView:View , leftIcon:ImageView ,
textView:TextView , item:SearchSuggestion , itemPosition:Int) {
            Log.d(TAG, "onBindSuggestion()"+itemPosition)
                val colorSuggestion =  item as ColorSuggestion

                val textColor =  "#000000"
                val textLight =  "#587878"
            
                leftIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.mipmap.icon_tabbar_lab_selected, null))
                textView.setTextColor(Color.parseColor(textColor))
           
            }

        })

      floating_search_view.setOnHomeActionClickListener(FloatingSearchView.OnHomeActionClickListener{
         hideKeyBoard()
         popBackStack()
         Log.d(TAG, "onHomeClicked()")
      }
)

    }

 }
